package main

import (
	"context"
	"log"
	"net"

	pb "go-grpc/proto"
	"google.golang.org/grpc"
)

const (
	port = ":50051"
)

// server 用于实现 proto.GreeterServer
type server struct {
	pb.UnimplementedGreeterServer
}

// SayHello 实现 proto.GreeterServer 接口
func (s *server) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
	log.Printf("收到请求: %v", req.GetName())
	return &pb.HelloReply{Message: "你好 " + req.GetName()}, nil
}

func main() {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("监听失败: %v", err)
	}
	s := grpc.NewServer()
	pb.RegisterGreeterServer(s, &server{})
	log.Printf("服务器监听在 %v", lis.Addr())
	if err := s.Serve(lis); err != nil {
		log.Fatalf("服务失败: %v", err)
	}
}
